package org.hawksee.core.data;

/*
 * This class holds a general region.
 */
public class Region
{
  private int x = 0;
  private int y = 0;
  private int width = 0;
  private int height = 0;
  
  public Region()
  {
    this(0, 0, 0, 0);
  }
  public Region(int x, int y, int width, int height) 
  {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }
  /*
   * Returns whether or not the region contains the (x,y) point
   */
  public boolean contains(int x, int y)
  {
    return ((x >= this.x) &&
            (y >= this.y) &&
            (x < (this.x + this.width)) &&
            (y < (this.y + this.height)));
  }
  /*
   * Expands the region by the amount specified.
   */
  public void expandBy(int amount)
  {
    this.x -= amount;
    this.y -= amount;
    this.width += 2 * amount;
    this.height += 2 * amount;
  }
  /*
   * Gets the area of the region
   */
  public int getArea()
  {
    return (this.width * this.height);
  }
  /*
   * Gets the bottom side of the region
   */
  public int getBottom()
  {
    return (this.y + this.height);
  }
  /*
   * Gets the center X value
   */
  public int getCenterX()
  {
    return (this.x + this.width / 2);
  }
  /*
   * Gets the center Y value
   */
  public int getCenterY()
  {
    return (this.y + this.height / 2);
  }
  /*
   * Gets the height of the region
   */
  public int getHeight()
  {
    return this.height;
  }
  /*
   * Gets the left side of the region
   */
  public int getLeft()
  {
    return this.x;
  }
  /*
   * Gets the right side of the region
   */
  public int getRight()
  {
    return (this.x + this.width);
  }
  /*
   * Gets the top of the region
   */
  public int getTop()
  {
    return this.y;
  }
  /*
   * Gets the width of the region
   */
  public int getWidth()
  {
    return this.width;
  }
  /*
   * Gets the x position of the region origin
   */
  public int getX()
  {
    return this.x;
  }
  /*
   * Gets the y position of the region origin
   */
  public int getY()
  {
    return this.y;
  }
  /*
   * Returns a new region resulting from intersecting with the input region
   */
  public Region intersection(Region region)
  {
    int left = Math.max(this.getLeft(), region.getLeft());
    int top = Math.max(this.getTop(), region.getTop());
    int right = Math.min(this.getRight(), region.getRight());
    int bottom = Math.min(this.getBottom(), region.getBottom());
    int width = (right > left) ? (right - left) : 0;
    int height = (bottom > top) ? (bottom - top) : 0;
    return new Region(left, top, width, height);
  }
  /*
   * Returns a new region resulting from the aggressive union of the two regions.
   */
  public Region union(Region region)
  {
    int left = Math.min(this.x, region.x);
    int top = Math.min(this.y, region.y);
    int right = Math.max(this.y, region.y);
    int bottom = Math.max(this.y, region.y);
    return new Region(top, left, right - left, bottom - top);
  }
}